/**
 * 
 */
package com.manthano.util.file;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.supercsv.io.CsvListReader;
import org.supercsv.prefs.CsvPreference;

/**
 * @author Vaibhav.Singh
 */
public class CSVFileReader implements FileReader
{
	private CsvListReader	csvReader	= null;

	/**
	 * @param inputStream
	 * @param charset
	 * @throws UnsupportedEncodingException
	 */
	public CSVFileReader(InputStream inputStream, String charset) throws UnsupportedEncodingException
	{
		csvReader = new CsvListReader(new InputStreamReader(inputStream, charset), CsvPreference.STANDARD_PREFERENCE);
	}

	public void close() throws IOException
	{
		if (csvReader != null)
		{
			csvReader.close();
			csvReader = null;
		}
	}

	public String[] readNext() throws IOException
	{
		List<String> list = csvReader.read();
		return list != null ? list.toArray(new String[list.size()]) : null;
	}
}
